我是面向对象编程的新手,但是我有这个Pong游戏,我想要添加的是让球随机生成。不要求任何人为我编写我的代码,只是想朝着正确的方向努力。
import pygame, sys, random
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
WINDOWWIDTH = 620
WINDOWHEIGHT = 440
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Pong')
pygame.font.init()
FPS = 60
class Player(object):
def __init__(self, x, y, padWid, padHei):
self.x, self.y = 0, WINDOWHEIGHT/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, (255, 255, 255))
windowSurface.blit(scoreBlit, (32, 16))
if self.score == 10:
print ("player 1 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 >= WINDOWHEIGHT-64:
self.y = WINDOWHEIGHT-64
def draw(self):
pygame.draw.rect(windowSurface, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei))
class Enemy(Player):
def __init__(self, x, y, padWid, padHei):
self.x, self.y = WINDOWWIDTH-8, WINDOWHEIGHT/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, (255, 255, 255))
windowSurface.blit(scoreBlit, (WINDOWHEIGHT+92, 16))
if self.score == 10:
print ("Player 2 wins!")
exit()
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
self.y -= self.speed
elif keys[pygame.K_DOWN]:
self.y += self.speed
if self.y <= 0:
self.y = 0
elif self.y >= WINDOWHEIGHT-64:
self.y = WINDOWHEIGHT-64
def draw(self):
pygame.draw.rect(windowSurface, (255, 255, 255), (self.x, self.y, self.padWid, self.padHei))
class Ball(object):
def __init__(self, x, y, size):
self.x, self.y = WINDOWWIDTH/2, WINDOWHEIGHT/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
#wall col
if self.y <= 0:
self.speed_y *= -1
elif self.y >= WINDOWHEIGHT-self.size:
self.speed_y *= -1
if self.x <= 0:
self.__init__(WINDOWWIDTH/2, WINDOWHEIGHT/2, 8)
enemy.score += 1
elif self.x >= WINDOWWIDTH-self.size:
self.__init__(WINDOWWIDTH/2, WINDOWHEIGHT/2, 8)
self.speed_x = 3
player.score += 1
for 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
for n in range(-self.size, enemy.padHei):
if self.y == enemy.y + n:
if self.x >= enemy.x - enemy.padWid:
self.speed_x *= -1
break
n += 1
def draw(self):
pygame.draw.rect(windowSurface, (255, 255, 255), (self.x, self.y, 8, 8))
def main():
global balls, BALLSIZE, windowSurface, player, ball, enemy, mainClock, FPS
player = Player('16', '2', '8', '64')
ball = Ball('2', '2', '8')
enemy = Enemy('-16', '2', '8' , '64')
while True:
#process
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
ball.movement()
player.movement()
enemy.movement()
windowSurface.fill((0, 0, 0))
ball.draw()
player.draw()
player.scoring()
enemy.draw()
enemy.scoring()
pygame.display.flip()
mainClock.tick(FPS)
main()
答案 0 :(得分:0)
如果你想让球随机产生,可以使用pygame.draw.circle()绘制乒乓球。
要随机生成它,导入Math,然后为pygame.draw.circle()第3个参数put
(Math.randint(1,Screen.width),Math.randint(1,Screen.height))
以下是文档: