1

## #新手 詢問錯誤原因

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.fill((255, 255, 255))
self.rect = self.image.get_rect()
self.rect.center = (inputx, inputy)
self.direction = random.randint(20, 70)
#設定界面

def update(self):
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))
controller = moveclip((255, 0, 0), 0, 350)
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)
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()
***```
``````

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()

``````

### 2 則留言

1
DanSnow
iT邦新手 1 級 ‧ 2021-06-09 00:47:03

0
screenleon
iT邦新手 3 級 ‧ 2021-06-09 08:46:08