0

Im working on a school project on a motion projectile but my while loop breaks before it ends.

import pygame, sys
from pygame.locals import *
from math import *

def rot_center(image, center_angle):
    orig_rect = image.get_rect()
    rot_image = pygame.transform.rotate(image, center_angle)
    rot_rect = orig_rect.copy()
    rot_rect.center = rot_image.get_rect().center
    rot_image = rot_image.subsurface(rot_rect).copy()
    return rot_image

pygame.init()

FPS = 144 
fpsClock = pygame.time.Clock()

SCENE = pygame.display.set_mode((1280, 720), 0, 32)
pygame.display.set_caption('Domača naloga')

backgroundImg = pygame.image.load('images/background.png')
cannonBaseImg = pygame.image.load('images/base.png')
cannonImg = pygame.image.load('images/cannon.png')
ballImg = pygame.image.load('images/cannonBall.png')
enemy = pygame.image.load('images/enemy.png')

cannonBasePos = (9,490)
cannonPos = (-5, 480)
ballPos = (22,510)
enemyPos = (40, 400)

cannonImg = pygame.transform.rotate(cannonImg, -15)

ang = 45
cannonMovImg = rot_center(cannonImg, ang)

# bug: backgoround image dissapears and it bugges green
SCENE.blit(backgroundImg, (0,0))
SCENE.blit(cannonMovImg, cannonPos  )
SCENE.blit(ballImg, ballPos)
SCENE.blit(cannonBaseImg, cannonBasePos)
SCENE.blit(enemy, enemyPos)

t = 0 
s = ballPos 
v = (0, 0) 
vm = 100 
launched = False 

while True: #<-- while starts here
    dt = fpsClock.tick(FPS) 
    if launched:
        t = t + dt/250.0 # cajt is clocka
        a = (0.0, 10.0) # acceleration
        v =  (v0[0] + a[0]*t, v0[1] + a[1]*t) # spazz out nad 1000 !!!
        vm = sqrt(v[0]*v[0] + v[1]*v[1])
        s0 = ballPos # poz
        s = (s0[0] + v0[0]*t + a[0]*t*t/2, s0[1] + v0[1]*t + a[1]*t*t/2)
        if s[1] >= 510: # tla
            launched = False

    font = pygame.font.Font(None, 30)

    text_ang = font.render("Kot = %d" % ang, 1, (10, 10, 10))
    text_ang_pos = (1050, 50)

    text_vm = font.render("Hitrost = %.1f m/s" % vm, 1, (10, 10, 10))
    text_vm_pos = (1050, 80)

    text_stat = font.render("Statistika:", 1, (10, 10, 10))
    text_stat_pos = (1050, 20)

    text_x = font.render("Dolžina = %.1f m" % s[0], 1, (10, 10, 10))
    text_x_pos = (1050, 110)

    text_t = font.render("Čas = %.1f s" % t, 1, (10, 10, 10))
    text_t_pos = (1050, 140)


    SCENE.blit(backgroundImg, (0,0))
    SCENE.blit(cannonMovImg, cannonPos)
    SCENE.blit(ballImg, s)
    SCENE.blit(cannonBaseImg, cannonBasePos)
    SCENE.blit(enemy, enemyPos) #<--- while loop breaks here
    SCENE.blit(text_t, text_t_pos)
    SCENE.blit(text_stat, text_stat_pos)
    SCENE.blit(text_vm, text_vm_pos)
    SCENE.blit(text_x, text_x_pos)
    SCENE.blit(text_ang, text_ang_pos)


    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                ballPos = (0,478)
                s = ballPos
                t = 0
                launched = True

                v0 = (vm*cos(radians(ang)), -vm*sin(radians(ang)))

    keystate = pygame.key.get_pressed()

    if keystate[K_LEFT]: 
        ang+=2
        if ang > 90:
            ang = 90
        cannonMovImg = rot_center(cannonImg, ang)

    if keystate[K_RIGHT]: 
        ang-=2
        if ang < 0:
            ang = 0
        cannonMovImg = rot_center(cannonImg, ang)

    if keystate[K_UP]: 
        vm+=2

    if keystate[K_DOWN]: 
        vm-=2

    pygame.display.flip() #<-- while should end here 

I've tried everything but even my teacher doesnt know the anwser. He said it's a bug in Python 3.6.3. Any ideas. I've researched for the past week and im frustrated. If anyone has any ideas please let me know. Thanks for your anwsers in advance

Sir Rage
  • 61
  • 8
  • 2
    I can't reproduce the error (I've only replaced the images with `pygame.Surface`s) and the program seems to work correctly. What exactly happens when you run the game? Does the window get closed immediately or do you have to press a key first? How do you run the game? Do you see any error messages? – skrx Oct 04 '17 at 20:52
  • Well if i run the game like it is right now it works only if this: SCENE.blit(enemy, enemyPos) stays as a comment, otherwise the While loop breaks at this location. For some reason. – Sir Rage Oct 05 '17 at 08:16
  • I found the solution there was a TAB space hidden before the SCENE and that was the problem. Thank you for your help anyway! – Sir Rage Oct 05 '17 at 10:43

0 Answers0