用户:狂妄之人lJM7查看:1 回复:1 评论:1 创建时间:2021-02-09T20:53:30
【作品展示】

【作品介绍】
我的第一个代码作品,没有任何bug,非常流畅
【作品源代码】
import pygame
from pygame.locals import *
import sys
import random
import time
import math
class GameWindow(object):
'''创建游戏窗口类'''
def __init__(self, *args, **kw):
self.window_length = 600
self.window_wide = 500
self.game_window = pygame.display.set_mode(
(self.window_length, self.window_wide))
pygame.display.set_caption("CatchBallGame")
self.window_color = (135, 206, 250)
def backgroud(self):
self.game_window.fill(self.window_color)
class Ball(object):
'''创建球类'''
def __init__(self, *args, **kw):
self.ball_color = (255, 215, 0)
self.move_x = 1
self.move_y = 1
self.radius = 10
def ballready(self):
self.ball_x = self.mouse_x
self.ball_y = self.window_wide-self.rect_wide-self.radius
pygame.draw.circle(self.game_window, self.ball_color,
(self.ball_x, self.ball_y), self.radius)
def ballmove(self):
pygame.draw.circle(self.game_window, self.ball_color,
(self.ball_x, self.ball_y), self.radius)
self.ball_x += self.move_x
self.ball_y -= self.move_y
self.ball_window()
self.ball_rect()
if self.distance < self.radius:
self.frequency += 1
if self.frequency == 5:
self.frequency = 0
self.move_x += self.move_x
self.move_y += self.move_y
self.point += self.point
if self.ball_y > 520:
self.gameover = self.over_font.render(
"Game Over", False, (0, 0, 0))
self.game_window.blit(self.gameover, (100, 130))
self.over_sign = 1
class Rect(object):
'''创建球拍类'''
def __init__(self, *args, **kw):
self.rect_color = (255, 0, 0)
self.rect_length = 100
self.rect_wide = 10
def rectmove(self):
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
if self.mouse_x >= self.window_length-self.rect_length//2:
self.mouse_x = self.window_length-self.rect_length//2
if self.mouse_x <= self.rect_length//2:
self.mouse_x = self.rect_length//2
pygame.draw.rect(self.game_window, self.rect_color, ((self.mouse_x-self.rect_length//2),
(self.window_wide-self.rect_wide), self.rect_length, self.rect_wide))
class Brick(object):
def __init__(self, *args, **kw):
self.brick_color = (139, 126, 102)
self.brick_list = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [
1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]]
self.brick_length = 80
self.brick_wide = 20
def brickarrange(self):
for i in range(5):
for j in range(6):
self.brick_x = j*(self.brick_length+24)
self.brick_y = i*(self.brick_wide+20)+40
if self.brick_list[i][j] == 1:
pygame.draw.rect(self.game_window, self.brick_color, (
self.brick_x, self.brick_y, self.brick_length, self.brick_wide))
self.ball_brick()
if self.distanceb < self.radius:
self.brick_list[i][j] = 0
self.score += self.point
if self.brick_list == [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]:
self.win = self.win_font.render("You Win", False, (0, 0, 0))
self.game_window.blit(self.win, (100, 130))
self.win_sign = 1
class Score(object):
'''创建分数类'''
def __init__(self, *args, **kw):
self.score = 0
self.score_font = pygame.font.SysFont('arial', 20)
self.point = 1
self.frequency = 0
def countscore(self):
my_score = self.score_font.render(
str(self.score), False, (255, 255, 255))
self.game_window.blit(my_score, (555, 15))
class GameOver(object):
'''创建游戏结束类'''
def __init__(self, *args, **kw):
self.over_font = pygame.font.SysFont('arial', 80)
self.over_sign = 0
class Win(object):
'''创建游戏胜利类'''
def __init__(self, *args, **kw):
self.win_font = pygame.font.SysFont('arial', 80)
self.win_sign = 0
class Collision(object):
'''碰撞检测类'''
def ball_window(self):
if self.ball_x <= self.radius or self.ball_x >= (self.window_length-self.radius):
self.move_x = -self.move_x
if self.ball_y <= self.radius:
self.move_y = -self.move_y
def ball_rect(self):
self.collision_sign_x = 0
self.collision_sign_y = 0
if self.ball_x < (self.mouse_x-self.rect_length//2):
self.closestpoint_x = self.mouse_x-self.rect_length//2
self.collision_sign_x = 1
elif self.ball_x > (self.mouse_x+self.rect_length//2):
self.closestpoint_x = self.mouse_x+self.rect_length//2
self.collision_sign_x = 2
else:
self.closestpoint_x = self.ball_x
self.collision_sign_x = 3
if self.ball_y < (self.window_wide-self.rect_wide):
self.closestpoint_y = (self.window_wide-self.rect_wide)
self.collision_sign_y = 1
elif self.ball_y > self.window_wide:
self.closestpoint_y = self.window_wide
self.collision_sign_y = 2
else:
self.closestpoint_y = self.ball_y
self.collision_sign_y = 3
self.distance = math.sqrt(math.pow(
self.closestpoint_x-self.ball_x, 2)+math.pow(self.closestpoint_y-self.ball_y, 2))
if self.distance < self.radius and self.collision_sign_y == 1 and (self.collision_sign_x == 1 or self.collision_sign_x == 2):
if self.collision_sign_x == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_x == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distance < self.radius and self.collision_sign_y == 1 and self.collision_sign_x == 3:
self.move_y = - self.move_y
if self.distance < self.radius and self.collision_sign_y == 3:
self.move_x = - self.move_x
def ball_brick(self):
self.collision_sign_bx = 0
self.collision_sign_by = 0
if self.ball_x < self.brick_x:
self.closestpoint_bx = self.brick_x
self.collision_sign_bx = 1
elif self.ball_x > self.brick_x+self.brick_length:
self.closestpoint_bx = self.brick_x+self.brick_length
self.collision_sign_bx = 2
else:
self.closestpoint_bx = self.ball_x
self.collision_sign_bx = 3
if self.ball_y < self.brick_y:
self.closestpoint_by = self.brick_y
self.collision_sign_by = 1
elif self.ball_y > self.brick_y+self.brick_wide:
self.closestpoint_by = self.brick_y+self.brick_wide
self.collision_sign_by = 2
else:
self.closestpoint_by = self.ball_y
self.collision_sign_by = 3
self.distanceb = math.sqrt(math.pow(
self.closestpoint_bx-self.ball_x, 2)+math.pow(self.closestpoint_by-self.ball_y, 2))
if self.distanceb < self.radius and self.collision_sign_by == 1 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 1 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 2 and (self.collision_sign_bx == 1 or self.collision_sign_bx == 2):
if self.collision_sign_bx == 1 and self.move_x > 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 1 and self.move_x < 0:
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x < 0:
self.move_x = - self.move_x
self.move_y = - self.move_y
if self.collision_sign_bx == 2 and self.move_x > 0:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 2 and self.collision_sign_bx == 3:
self.move_y = - self.move_y
if self.distanceb < self.radius and self.collision_sign_by == 3:
self.move_x = - self.move_x
class Main(GameWindow, Rect, Ball, Brick, Collision, Score, Win, GameOver):
'''创建主程序类'''
def __init__(self, *args, **kw):
super(Main, self).__init__(*args, **kw)
super(GameWindow, self).__init__(*args, **kw)
super(Rect, self).__init__(*args, **kw)
super(Ball, self).__init__(*args, **kw)
super(Brick, self).__init__(*args, **kw)
super(Collision, self).__init__(*args, **kw)
super(Score, self).__init__(*args, **kw)
super(Win, self).__init__(*args, **kw)
start_sign = 0
while True:
self.backgroud()
self.rectmove()
self.countscore()
if self.over_sign == 1 or self.win_sign == 1:
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
pressed_array = pygame.mouse.get_pressed()
if pressed_array[0]:
start_sign = 1
if start_sign == 0:
self.ballready()
else:
self.ballmove()
self.brickarrange()
pygame.display.update()
time.sleep(0.010)
if __name__ == '__main__':
pygame.init()
pygame.font.init()
catchball = Main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn