Difficulty with movement in game close to Pong (pygame) -
note: new in programming.
at first, going post code have written:
import pygame import random pygame.init() screen = pygame.display.set_mode([640, 480]) paddle = pygame.image.load("pulgake.png") pygame.display.set_caption("pong!") white = [0, 0, 0] = pygame.image.load("taust.png") screen.blit(back, (0, 0)) screen.blit(paddle, (600, 240)) pygame.display.flip() xpaddle = 600 ypaddle = 240 delay = 10 interval = 10 pygame.key.set_repeat(delay, interval) class ball: def __init__(self): self.pos = [0,0] self.velocity = [1,0] def move(): self.pos[0] += self.velocity[0] self.pos[1] += self.velocity[1] if self.pos[0] > 10: self.pos[0] *= -1 elif self.pos[1] > 470: # background has frame self.pos[1] *= -1 elif self.pos[1] < 10: self.pos[1] *= -1 elif self.pos[0] > 605: false def draw(screen): pygame.draw.circle(screen,white,self.pos,15) pall = ball() while true: pall.move() screen.blit(back, (0,0)) screen.blit(paddle, (xpaddle, ypaddle)) pygame.display.flip() in pygame.event.get(): if i.type == pygame.quit: sys.exit() elif i.type == pygame.keydown: if i.key == pygame.k_up: ypaddle = ypaddle - 10 if ypaddle < 10: ypaddle = 10 elif i.key == pygame.k_down: ypaddle = ypaddle + 10 if ypaddle > 410: ypaddle = 410
i getting error-
line 44, in <module> pall.move() typeerror: move() takes no arguments (1 given)
so, want ball bounces off walls (and paddle well, don't know yet how)
also, grateful if helps me stopwatch in game, showing time "survived".
edit: added def move(self)
now, bigger problem occured:
draw() , move() - global name"self" not defined. can't figure out one.
you calling ball.move
doesn't exist, because move
needs indented once more.
if want "do stuff while key held down" can use getkeystate
, use keydown events when want 'do stuff once, when key pressed'
see both here: https://stackoverflow.com/a/13207525/341744
Comments
Post a Comment