因作业缘故,上网找了打击砖块的游戏,需要加入自己的元素进去
目前想法是增加击中第五球後球速变快,但在这段中不断出现错误,想询问解决办法谢谢
***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.direction
def 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 = True
if 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()
<<: 伸缩自如的Flask [day 25] Flask with web cam
上传本机端的档案其实很简单,只是单纯的 setValue() 就好了。 browser.setVal...
当我们有很多重复的架构,内容却不一样,以旧有无框架的开发,我们可能就需要手动一笔一笔的刻出来,更进步...
1.抓资料 > 汇入 CREATE DATABASE 後汇入 https://github.c...
Q: 说好的文字Loading呢? A: 客倌这里上菜罗~ 前几篇的Loading都是以图案循环为...
在简单介绍完驻列後,我要将原本的寄信流程改为使用驻列来寄信。 使用资料库做驻列 env.档案中的QU...