python游戏模块 - 26 小项目-坦克行动 / 大球吃小球
生活随笔
收集整理的這篇文章主要介紹了
python游戏模块 - 26 小项目-坦克行动 / 大球吃小球
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import pygame from sys import exit from enum import Enum, unique ? """e_enum->enum->Enum""" ? ? ?# 保證這個中的類的這些屬性的值是唯一的 class Key_value(Enum): ? ?UP_KEY = 273 ? ?DOWN_KEY = 274 ? ?RIGHT_KEY = 275 ? ?LEFT_KEY = 276 ? ?# KEY = 276 ? ? class Tank: ? ? ?def __init__(self, x=0, y=0, x_speed=0, y_speed=0): ? ? ? ?self.x = x ? ? ? ?self.y = y ? ? ? ?self.x_speed = x_speed ? ? ? ?self.y_speed = y_speed ? ? ? ?self.image = '' ? ? ? ?self.width = 80 ? ? ? ?self.height = 80 ? ? ?def show(self, place): ? ? ? ?image = pygame.image.load(self.image) ? ? ? ?image = pygame.transform.scale(image, (self.width, self.height)) ? ? ? ?place.blit(image, (self.x, self.y)) ? ? ?def move(self): ? ? ? ?self.x += self.x_speed ? ? ? ?self.y += self.y_speed ? ? def main(): ? ?pygame.init() ? ? ?# 創建窗口 ? ?screen = pygame.display.set_mode((480, 600)) ? ? ?# 設置窗口的背景顏色 ? ?screen.fill((255, 255, 255)) ? ? ?# 創建坦克 ? ?tank = Tank() ? ?tank.x = 480 / 2 - tank.width / 2 ? ?tank.y = 600 / 2 - tank.height / 2 ? ?tank.image = 'p1tankU.gif' ? ?tank.show(screen) ? ? ?pygame.display.flip() ? ? ?# 坦克是否移動 ? ?flag = False ? ? ?while True: ? ? ? ?for event in pygame.event.get(): ? ? ? ? ? ?if event.type == pygame.QUIT: ? ? ? ? ? ? ? ?exit() ? ? ? ? ? ? ?if event.type == pygame.KEYUP: ? ? ? ? ? ? ? ?flag = False ? ? ? ? ? ? ?if event.type == pygame.KEYDOWN: ? ? ? ? ? ? ? ?flag = True ? ? ? ? ? ? ? ?if event.key == Key_value.UP_KEY.value: ? ? ? ? ? ? ? ? ? ?tank.image = 'p1tankU.gif' ? ? ? ? ? ? ? ? ? ?tank.x_speed = 0 ? ? ? ? ? ? ? ? ? ?tank.y_speed = -1 ? ? ? ? ? ? ? ?elif event.key == Key_value.DOWN_KEY.value: ? ? ? ? ? ? ? ? ? ?tank.image = 'p1tankD.gif' ? ? ? ? ? ? ? ? ? ?tank.x_speed = 0 ? ? ? ? ? ? ? ? ? ?tank.y_speed = 1 ? ? ? ? ? ? ? ?elif event.key == Key_value.LEFT_KEY.value: ? ? ? ? ? ? ? ? ? ?tank.image = 'p1tankL.gif' ? ? ? ? ? ? ? ? ? ?tank.y_speed = 0 ? ? ? ? ? ? ? ? ? ?tank.x_speed = -1 ? ? ? ? ? ? ? ?elif event.key == Key_value.RIGHT_KEY.value: ? ? ? ? ? ? ? ? ? ?tank.image = 'p1tankR.gif' ? ? ? ? ? ? ? ? ? ?tank.y_speed = 0 ? ? ? ? ? ? ? ? ? ?tank.x_speed = 1 ? ? ? ? ?if flag: ? ? ? ? ? ?screen.fill((255, 255, 255)) ? ? ? ? ? ?tank.move() ? ? ? ? ? ?tank.show(screen) ? ? ? ? ? ?pygame.display.flip() ? ? if __name__ == '__main__': ? ?main() ?
2. 小項目 - 大球吃小球
import pygame from sys import exit from random import randint ? # 開方函數 from math import sqrt ? SCREEN_WIDTH = 800 SCREEN_HEIGHT = 500 ? ? # 球類 class Ball: ? ?"""球類""" ? ?all_ball = [] ? ? ?def __init__(self, x=0, y=0, radius=0, color=(255, 0, 0)): ? ? ? ?self.x = x ? ? ? ?self.y = y ? ? ? ?self.x_seed = 0 ? ? ? ?self.y_seed = 0 ? ? ? ?self.radius = radius ? ? ? ?self.color = color ? ? ?def move(self): ? ? ? ?self.x += self.x_seed ? ? ? ?self.y += self.y_seed ? ? ? ?if self.x < self.radius or self.x > SCREEN_WIDTH - self.radius: ? ? ? ? ? ?self.x_seed = -self.x_seed ? ? ? ? ?if self.y < self.radius or self.y > SCREEN_HEIGHT - self.radius: ? ? ? ? ? ?self.y_seed = -self.y_seed ? ? ?"""判斷當前的小球是否和另外的一個小球相撞""" ? ? ?def crash(self, other): ? ? ? ?dx = self.x - other.x ? ? ? ?dy = self.y - other.y ? ? ? ?distance = sqrt(dx ** 2 + dy ** 2) ? ? ? ? ?if distance < self.radius + other.radius: ? ? ? ? ? ?value = randint(0, 1) ? ? ? ? ? ?if value == 0: ? ? ? ? ? ? ? ?# 繼承被吃的球的半徑 ? ? ? ? ? ? ? ?self.radius += int(other.radius * 0.4) ? ? ? ? ? ? ? ?# 讓被吃的球消失 ? ? ? ? ? ? ? ?Ball.all_ball.remove(other) ? ? ? ? ? ?else: ? ? ? ? ? ? ? ?other.radius += int(self.radius * 0.4) ? ? ? ? ? ? ? ?Ball.all_ball.remove(self) ? ? # 顏色類 class Color: ? ?"""顏色類""" ? ?RED = (255, 0, 0) ? ?GREEN = (0, 255, 0) ? ?WHITE = (255, 255, 255) ? ?BLACK = (0, 0, 0) ? ?GRAY = (100, 100, 100) ? ? ? ? ?def rand_color(): ? ? ? ?return randint(0, 255), randint(0, 255), randint(0, 255) ? ? # ==========game_loop========== def main(): ? ?pygame.init() ? ?screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) ? ?pygame.display.set_caption('小球碰撞') ? ? ?while True: ? ? ? ? ?for event in pygame.event.get(): ? ? ? ? ? ?if event.type == pygame.QUIT: ? ? ? ? ? ? ? ?exit() ? ? ? ? ? ? ?if event.type == pygame.MOUSEBUTTONDOWN: ? ? ? ? ? ? ? ?# 鼠標點擊需要產生一個球 ? ? ? ? ? ? ? ?born_ball(event.pos) ? ? ? ? ?pygame.time.delay(50) ? ? ? ?# 將球渲染到界面上(刷新界面) ? ? ? ?show_ball(screen) ? ? ? ? ?# 讓小球都動起來 ? ? ? ?move_all_ball() ? ? ? ? ?# 去檢測界面的球的碰撞 ? ? ? ?all_ball_crash() ? ? """球的碰撞檢測""" ? ? def all_ball_crash(): ? ?# 先拿出一個球 ? ?for ball in Ball.all_ball: ? ? ? ?# 拿另外一個球,不能是第一個 ? ? ? ?for ball2 in Ball.all_ball: ? ? ? ? ? ?if ball != ball2: ? ? ? ? ? ? ? ?ball.crash(ball2) ? ? """球動起來""" ? ? def move_all_ball(): ? ?for ball in Ball.all_ball: ? ? ? ?ball.move() ? ? # 講球畫到指定的位置 place:地方 def show_ball(place): ? ?# 清屛 ? ?place.fill(Color.WHITE) ? ?# 畫球 ? ?for ball in Ball.all_ball: ? ? ? ?pygame.draw.circle(place, ball.color, (ball.x, ball.y), ball.radius) ? ? ?pygame.display.flip() ? ? # 在指定的位置產生一個球 def born_ball(pos): ? ?x, y = pos ? ?r = randint(5, 15) ?# 隨機產生球的半徑 ? ?ball_color = Color.rand_color() ?# 隨機產生球的顏色 ? ? ?# 創建了對應的球 ? ?ball = Ball(x, y, r, ball_color) ? ?ball.x_seed = randint(-8, 8) ? ?ball.y_seed = randint(-8, 8) ? ?# 將球保存起來 ? ?Ball.all_ball.append(ball) ? ? if __name__ == '__main__': ? ?main() ?總結
以上是生活随笔為你收集整理的python游戏模块 - 26 小项目-坦克行动 / 大球吃小球的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis缓存机制之一级缓存
- 下一篇: CF1278B A and B