因作业缘故,上网找了打击砖块的游戏,需要加入自己的元素进去
目前想法是增加击中第五球后球速变快,但在这段中不断出现错误,想询问解决办法谢谢
***if self.distdistance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.movex += self.movex
self.movey += self.movey
self.point += self.point *
以下为完整程式码
import pygame
import random
import math
import time
class ball(pygame.sprite.Sprite):#球
direction = 0
speed = 0
x = 0
y = 0
dx = 0
dy = 0
def __init__(self, inputspeed, inputx, inputy, radius, color): # pygame.sprite.Sprite.__init__(self) self.speed = inputspeed self.x = inputx self.y = inputy self.image = pygame.Surface([radius * 2, radius * 2]) self.image.fill((255, 255, 255)) pygame.draw.circle(self.image, color, (radius, radius), radius, 0) self.rect = self.image.get_rect() self.rect.center = (inputx, inputy) self.direction = random.randint(20, 70) #设定界面def update(self): radian = math.radians(self.direction) self.dx = self.speed * math.cos(radian) self.dy = -self.speed * math.sin(radian) self.x += self.dx self.y += self.dy self.rect.x = self.x self.rect.y = self.y if (self.rect.left <= 0 or self.rect.right >= window.get_width()): self.hitleftright() elif (self.rect.top <= 0): self.hittop()def hittop(self): self.direction = 360 - self.directiondef hitleftright(self): if 180 >= self.direction >= 0: self.direction = 180 - self.direction else: self.direction = 540 - self.direction
class brick(pygame.sprite.Sprite):#砖头
def init(self, color, x, y):
pygame.sprite.Sprite.init(self)
self.image = pygame.Surface([40, 15])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class moveclip(pygame.sprite.Sprite):#横槓
def init(self, color, x, y):
pygame.sprite.Sprite.init(self)
self.image = pygame.Surface([20, 80])#长宽 更改
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self): pos = pygame.mouse.get_pos() self.rect.x = pos[0] if self.rect.x > window.get_width() - self.rect.width: self.rect.x = window.get_width() - self.rect.width
pygame.init()
font = pygame.font.SysFont("SimHei", 20)
def gameover(message):
global run
text = font.render(message, 1, (255, 0, 0))
window.blit(text, (window.get_width() / 2 - 150, window.get_height() / 2))
pygame.display.update()
run = False
time.sleep(3)
point = 0
window = pygame.display.set_mode((600, 400))
pygame.display.set_caption("breakingbricks")
background = pygame.Surface(window.get_size())
background = background.convert()
background.fill((255, 255, 255))
window.blit(background, (0, 0))
allsprite = pygame.sprite.Group()
bricksprite = pygame.sprite.Group()
controllersprite = pygame.sprite.Group()
theball = ball(6, 100, 100, 20, (0, 0, 255))
allsprite.add(theball)
controller = moveclip((255, 0, 0), 0, 350)
allsprite.add(controller)
controllersprite.add(controller)
clock = pygame.time.Clock()
for i in range(3):
for j in range(0, 15):
thebrick = brick((random.randint(0, 255), random.randint(
0, 255), random.randint(0, 255)), j * 40 + 1, i * 15 + 1)
bricksprite.add(thebrick)
allsprite.add(thebrick)
msgstart = "Push Ready!"
msgstartdisplay = font.render(msgstart, 100, (255, 0, 0))
window.blit(msgstartdisplay,
(window.get_width() / 2 - 50, window.get_height() / 2))
playing = False
run = True
while run:
clock.tick(40)
for event in pygame.event.get():
if event.type == pygame.QUIT: #
running = False
button = pygame.mouse.get_pressed()
if button[0]: playing = Trueif playing: window.blit(background, (0, 0)) if theball.y >= window.get_height(): gameover("You Die") controller.update() hitbrick = pygame.sprite.spritecollide(theball, bricksprite, True) if len(hitbrick) > 0: point += len(hitbrick) theball.rect.y += 20 theball.hittop() if len(bricksprite) == 0: gameover("You Die") hitclip = pygame.sprite.spritecollide(theball, controllersprite, False) if len(hitclip) > 0: theball.hittop() theball.update()***```
if self.distdistance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.movex += self.movex
self.movey += self.movey
self.point += self.point ***
allsprite.draw(window) msgscore = "score: " + str(point) msgscoredisplay = font.render(msgscore, 5, (255, 0, 0)) window.blit(msgscoredisplay, (window.get_width() - 60, window.get_height() - 30)) pygame.display.update()pygame.quit()