import pygame, random, time
# main function where we call all other functions, start the game loop, quit pygame and clean up the window. Inside we create a game object, display surface, and start the game loop by calling the play method on the game object. There is also a set caption with the title of the game.
def main():
pygame.init()
size =(500,400)
surface=pygame.display.set_mode(size)
pygame.display.set_caption('Pong v2')
game = Game(surface)
game.play()
center_ball=self.ball.center
pygame.quit()
# This is where we define the class game
class Game():
# an object in this class represents a complete game
# here we initialize a game. self is the game that is initialized surface is the display window surface object we also set default values for continuing the game and closing the window. we also define what fps we are running the game at, and define the velocity color position and radius of the ball
def __init__(self,surface):
# defining surface, fps, background color
self.surface=surface
self.FPS=120
self.bg_color=pygame.Color('black')
screen_width = surface.get_width()
screen_height = surface.get_height()
# defining ball attributes
ball_radius=10
ball_pos = [random.randint(ball_radius, screen_width-ball_radius),
random.randint(ball_radius, screen_height-ball_radius)]
ball_color=pygame.Color('white')
ball_velocity=[2,1]
self.ball=Ball(ball_pos,ball_radius,ball_color,ball_velocity,surface)
# defining paddle attributes
rect_left=[50,450]
rect_top=225
rect_height=60
rect_width=10
self.Paddle1=Rect(rect_left[0],rect_top,rect_width,rect_height,surface)
self.Paddle2=Rect(rect_left[1],rect_top,rect_width,rect_height,surface)
self.game_Clock=pygame.time.Clock()
self.close_clicked=False
self.continue_game=True
self.score1=0
self.score2=0
self.frame_counter=0
#return ball_radius
def play(self):
# game is played until player clicks close and until score1/score2 reaches 11
while not self.close_clicked and self.score1 <=11 and self.score2 <=11:
self.handle_events()
self.draw()
# if nothing sets this to false the game will continue to update
if self.continue_game:
self.update()
self.game_Clock.tick(self.FPS)
# score is drawn onto the screen (unimportant this is just playing with a feature for the next version), we define color font background etc of the score message and update score upon points being scored
def draw_score(self):
font_color = pygame.Color("white")
font_bg = pygame.Color("black")
font = pygame.font.SysFont("arial", 18)
text_img = font.render("Score for Player 1: " + str(self.score1) + ' Score for Player 2: ' + str(self.score2), True, font_color, font_bg)
text_pos = (0,0)
self.surface.blit(text_img, text_pos)
# ball, surface, score, and two paddles are drawn, pygame also updates this drawing once per frame
def draw(self):
self.surface.fill(self.bg_color)
self.draw_score()
self.Paddle1.draw()
self.Paddle2.draw()
self.ball.draw()
pygame.display.update()
# score value set to default of 0 we tell ball to move and add 1 to frame counter upon each update. update game object for the next frame
def update(self):
self.ball.move()
self.score=0
#get_ball_radius=self.init()
get_self_center=self.ball.move()
if get_self_center[0] + ball_radius <=0:
score1+=1
if get_self_center[0] + ball_radius>=sizee[0]:
score2+=1
if self.Paddle1.collide_point(get_self_center[0],get_self_center[1])or self.Paddle2.collide_point(get_self_center[0],get_self_center[1])==True:
self.ball.bounce()
#if self.Paddle1.collide_point(self.ball.move[0],self.ball.move[1]) == True:
#game.ball.velocity=-game.ball.velocity
#if self.rect.collidepoint(Game.ball.center[0],Game.ball.center[1])==True:
#pass
self.frame_counter+=self.frame_counter+1
# here we setup an event loop and figure out if the action to end the game has been done
def handle_events(self):
events=pygame.event.get()
for event in events:
if event.type== pygame.QUIT:
self.close_clicked=True
# user defined class ball
class Ball:
# self is the ball to intialize. color/center/radius are defined for the ball that is initialized
def __init__(self,center,radius,color,velocity,surface):
self.center=center
self.radius=radius
self.color=color
self.velocity=velocity
self.surface=surface
# screen size is determined and edge of ball is checked that it is not touching the edge. if it is touching the edge it bounces and reverses velocity
def move(self):
screen_width=self.surface.get_width()
screen_height=self.surface.get_height()
screen_size=(screen_width,screen_height)
for i in range(0,len(self.center)):
#print(self.center)
self.center[i]+=self.velocity[i]
if (self.center[i]<=0 + self.radius or self.center[i]>=screen_size[i] - self.radius):
self.velocity[i]=-self.velocity[i]
return self.center
def bounce(self):
self.velocity=-self.velocity
# ball is drawn
def draw(self):
self.circle=pygame.draw.circle(self.surface,self.color,self.center,self.radius)
class Rect:
def __init__(self,left,top,width,height,surface):
#self.left=left
#self.top=top
#self.width=width
#self.height=height
self.surface=surface
self.rect=pygame.Rect(left,top,width,height)
def draw(self):
pygame.draw.rect(self.surface,pygame.Color('red'),self.rect)
def collide_point(self, x, y,):
get_self_center=self.ball.move()
pygame.Rect.collidepoint(get_self_center)
#print(self.move())
main()
我正在尝试制作经典的街机游戏pong,但我无法使其正常工作。得分功能由于某种原因不会采用球半径+中心,以便我在球员得分时更新得分。当我尝试检测球与球拍之间的碰撞以使其弹回时,无论我进行任何更改,碰撞检测都不会停止向我返回错误。它一直说像Rect并不是Rect的属性,或者我想在需要pygame.Rect对象时传递一个int值。有人可以解释出什么问题了吗?面向对象的编程对我来说超级混乱
答案 0 :(得分:0)
您似乎并没有真正的“游戏循环”来处理这些动作。游戏循环在Python中看起来像这样(这只是一个骨架):
ALTER TABLE `calendar` ADD PRIMARY KEY (`CalendarKey`);