Python/Pygame Trouble with Game loop/Getting code to run -
for past few days i've been working on code pong , i've got running. works movement of objects. screen freezes when pygame window opens. there seems wrong in main() still new python/pygame , lost wrong.
import pygame, sys pygame.init() scr_wid, scr_hei = 640, 480 class player(): def __init__(self): self.x, self.y = 16, scr_hei/2 self.x1, self.y1 = scr_wid-16, scr_hei/2 self.speed = 3 self.padwid, self.padhei = 8, 64 self.score = 0 self.scorefont = pygame.font.font("imagine_font.ttf", 64) def scoring(self): scoreblit = self.scorefont.render(str(self.score), 1, (0,0,0)) screen.blit(scoreblit, (32, 16)) if self.score == 10: print ("player 1 wins!") exit() def scoring1(self): scoreblit = self.scorefont.render(str(self.score), 1, (0,0,0)) screen.blit(scoreblit, (scr_hei+92, 16)) if self.score == 10: print ("player 2 wins!") exit() def movement(self): keys = pygame.key.get_pressed() if keys[pygame.k_w]: self.y -= self.speed elif keys[pygame.k_s]: self.y += self.speed if self.y <= 0: self.y = 0 elif self.y >= scr_hei-64: self.y = scr_hei-64 keys1 = pygame.key.get_pressed() if keys1[pygame.k_up]: self.y1 -= self.speed elif keys1[pygame.k_down]: self.y1 += self.speed if self.y1 <= 0: self.y1 = 0 elif self.y >= scr_hei-64: self.y1 = scr_hei-64 def draw(self): pygame.draw.rect(screen, (0,0,0), (self.x, self.y, self.padwid, self.padhei)) pygame.draw.rect(screen, (0,0,0), (self.x1, self.y1, self.padwid, self.padhei)) class ball(): def __init__(self): self.x, self.y = scr_wid/2, scr_hei/2 self.x1, self.y1 = scr_wid/2, scr_hei/2 self.speed_x = -3 self.speed_y = 3 self.size = 8 def movement(self): self.x += self.speed_x self.y += self.speed_y if self.y <= 0: self.speed_y *= -1 elif self.y >= scr_hei-self.size: self.speed_y *= -1 if self.x <= 0: self.__init__() enemy.score += 1 elif self.x >= scr_wid-self.size: self.__init__() self.speed_x = 3 player.score += 1 n in range(-self.size, player.padhei): if self.y == player.y + n: if self.x <= player.x + player.padwid: self.speed_x *= -1 break n += 1 self.x1 += self.speed_x self.y1 += self.speed_y if self.y1 <= 0: self.speed_y *= -1 elif self.y1 >= scr_hei-self.size: self.speed_y *= -1 if self.x1 <= 0: self.__init__() enemy.score += 1 elif self.x1 >= scr_wid-self.size: self.__init__() self.speed_x = 3 player.score += 1 n in range(-self.size, enemy.padhei): if self.y1 == enemy.y1 + n: if self.x1 >= enemy.x1 - enemy.padwid: self.speed_x *= -1 break n += 1 def draw(self): pygame.draw.rect(screen, (112,138,144), (self.x, self.y, 8, 8)) scr_wid, scr_hei = 640, 480 screen = pygame.display.set_mode((scr_wid, scr_hei)) player = player() ball = ball() enemy = player() def main(): running = true while running == true: event in pygame.event.get(): if event.type == pygame.quit: print ("game exited user") exit() else: running = false pygame.font.init() clock = pygame.time.clock() fps = 60 backgroundimage = pygame.image.load("background.png") pygame.display.set_caption("justin's pong") ball.movement() player.movement() enemy.movement() position = (0,0) screen.fill((0, 0, 0)) screen.blit(backgroundimage, position) ball.draw() player.draw() player.scoring() enemy.draw() enemy.scoring1() pygame.display.flip() clock.tick(fps) main()
the problem code:
running = true while running == true: event in pygame.event.get(): if event.type == pygame.quit: print ("game exited user") exit() else: running = false
you correctly getting events, if there aren't events (as there aren't in beginning), run forever, checking events.
i assume want rest of code run in while loop well, should indent it.
Comments
Post a Comment