Compare commits

..

No commits in common. 'ae9657dff245429d84996d70bb68e2ac3da52db9' and '047ccd0540d42b7dd5f5daa9c223bdcac83d2341' have entirely different histories.

8
.idea/.gitignore vendored

@ -1,8 +0,0 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" project-jdk-name="Python 3.11" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/google.iml" filepath="$PROJECT_DIR$/.idea/google.iml" />
</modules>
</component>
</project>

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 575 B

@ -1,6 +1,5 @@
from dinosaur import * from dinosaur import *
class Bullet: class Bullet:
def __init__(self, game_speed, dinosaur): def __init__(self, game_speed, dinosaur):
self.game_speed = game_speed self.game_speed = game_speed
@ -11,6 +10,5 @@ class Bullet:
def update(self): def update(self):
self.rect.x += self.game_speed self.rect.x += self.game_speed
def draw(self,SCREEN): def draw(self,SCREEN):
SCREEN.blit(self.image, (self.rect.x, self.rect.y)) SCREEN.blit(self.image, (self.rect.x, self.rect.y))

@ -1,7 +1,5 @@
import random
from settings import * from settings import *
import random
class Cloud: class Cloud:
def __init__(self, game_speed): def __init__(self, game_speed):

@ -1,5 +1,5 @@
from settings import * from settings import *
from bullet import Bullet
class Dinosaur: class Dinosaur:
X_ori = 80 # 恐龙的初始x坐标 X_ori = 80 # 恐龙的初始x坐标
@ -16,6 +16,7 @@ class Dinosaur:
self.gravity = 9.8 self.gravity = 9.8
self.hp = 2 self.hp = 2
# 恐龙的三种状态:低头,奔跑,跳跃 # 恐龙的三种状态:低头,奔跑,跳跃
self.status = 1 # 状态机0表示低头 1表示奔跑 2表示跳跃状态, 3表示往左移动 4表示往右移动 self.status = 1 # 状态机0表示低头 1表示奔跑 2表示跳跃状态, 3表示往左移动 4表示往右移动

@ -3,15 +3,15 @@ import sys
import pygame.time import pygame.time
from bullet import Bullet
from cloud import Cloud
from dinosaur import Dinosaur
from settings import * from settings import *
from dinosaur import Dinosaur
from cloud import Cloud
from bullet import Bullet
pygame.init() # 初始化pygame pygame.init() # 初始化pygame
pygame.display.set_caption('谷歌小游戏(小恐龙快跑)') # 设置标题 pygame.display.set_caption('谷歌小游戏(小恐龙快跑)') # 设置标题
class Barrier: # 定义一个障碍物基类, 后面的各个障碍物都是Barrier的子类 class Barrier: # 定义一个障碍物基类, 后面的各个障碍物都是Barrier的子类
def __init__(self, image, type): def __init__(self, image, type):
self.image = image self.image = image
@ -20,7 +20,6 @@ class Barrier: # 定义一个障碍物基类, 后面的各个障碍物都是B
self.rect.x = SCREEN_WIDTH # 先把障碍物放到地图的右下角,然后出来 self.rect.x = SCREEN_WIDTH # 先把障碍物放到地图的右下角,然后出来
self.game_speed = game_speed self.game_speed = game_speed
self.hp = 100 # 血量 self.hp = 100 # 血量
def update(self): def update(self):
self.rect.x -= self.game_speed # x坐标向左移动即障碍物向左移动 self.rect.x -= self.game_speed # x坐标向左移动即障碍物向左移动
if self.rect.x < -self.rect.width or self.hp <= 0: # 还需要判断障碍物是否移出了边界,如果越界的话我们要把该障碍物释放掉 if self.rect.x < -self.rect.width or self.hp <= 0: # 还需要判断障碍物是否移出了边界,如果越界的话我们要把该障碍物释放掉
@ -39,8 +38,6 @@ class Barrier: # 定义一个障碍物基类, 后面的各个障碍物都是B
font = pygame.font.SysFont(['方正粗黑宋简体', 'microsoftsansserif'], 30) font = pygame.font.SysFont(['方正粗黑宋简体', 'microsoftsansserif'], 30)
text = font.render("┗|`O'|┛", True, (0, 0, 0)) text = font.render("┗|`O'|┛", True, (0, 0, 0))
SCREEN.blit(text, (self.rect.x, self.rect.y - 70)) SCREEN.blit(text, (self.rect.x, self.rect.y - 70))
# barriers的子类 # barriers的子类
class SmallCactus(Barrier): class SmallCactus(Barrier):
def __init__(self, image): def __init__(self, image):
@ -48,14 +45,12 @@ class SmallCactus(Barrier):
super().__init__(image, self.type) # super调用父类对象的方法 super().__init__(image, self.type) # super调用父类对象的方法
self.rect.y = 325 self.rect.y = 325
class LargeCactus(Barrier): class LargeCactus(Barrier):
def __init__(self, image): def __init__(self, image):
self.type = random.randint(0, 2) self.type = random.randint(0, 2)
super().__init__(image, self.type) super().__init__(image, self.type)
self.rect.y = 300 self.rect.y = 300
class Bird(Barrier): class Bird(Barrier):
def __init__(self, image): def __init__(self, image):
self.type = 0 self.type = 0
@ -70,7 +65,6 @@ class Bird(Barrier):
self.index = 0 self.index = 0
SCREEN.blit(self.image[self.index // 5], self.rect) SCREEN.blit(self.image[self.index // 5], self.rect)
self.index += 1 self.index += 1
def update(self): # 多态实现鸟的上下移动 def update(self): # 多态实现鸟的上下移动
self.rect.x -= self.game_speed self.rect.x -= self.game_speed
if self.rect.x < -self.rect.width or self.hp <= 0: # 还需要判断障碍物是否移出了边界,如果越界的话我们要把该障碍物释放掉 if self.rect.x < -self.rect.width or self.hp <= 0: # 还需要判断障碍物是否移出了边界,如果越界的话我们要把该障碍物释放掉
@ -82,8 +76,6 @@ class Bird(Barrier):
self.vy = -self.vy self.vy = -self.vy
elif self.rect.y >= SCREEN_HEIGHT - 250: elif self.rect.y >= SCREEN_HEIGHT - 250:
self.vy = 10 self.vy = 10
def menu(death_cnt): def menu(death_cnt):
global points, max_score# 引入points, max_score全局变量 global points, max_score# 引入points, max_score全局变量
run = True run = True
@ -91,6 +83,8 @@ def menu(death_cnt):
SCREEN.fill((255, 255, 255)) # 背景色设置为白色 SCREEN.fill((255, 255, 255)) # 背景色设置为白色
font = pygame.font.SysFont('microsoftyahei', 50)# 微软雅黑 font = pygame.font.SysFont('microsoftyahei', 50)# 微软雅黑
if death_cnt == 0: if death_cnt == 0:
text = font.render("按任意键开始", True, (0, 0, 0)) text = font.render("按任意键开始", True, (0, 0, 0))
elif death_cnt > 0: elif death_cnt > 0:
@ -113,8 +107,7 @@ def menu(death_cnt):
SCREEN.blit(RUNNING[0], (SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT // 2 - 140)) #把恐龙的图片显示在菜单界面 SCREEN.blit(RUNNING[0], (SCREEN_WIDTH // 2 - 50, SCREEN_HEIGHT // 2 - 140)) #把恐龙的图片显示在菜单界面
# 添加作者信息 # 添加作者信息
author_font = pygame.font.SysFont('microsoftyahei', 30)# 创建一个字体对象,使用 'microsoftyahei' 字体,字号大小为 30 author_font = pygame.font.SysFont('microsoftyahei', 30)# 创建一个字体对象,使用 'microsoftyahei' 字体,字号大小为 30
author_text = author_font.render("作者: 袁恒", True, author_text = author_font.render("作者: 袁恒", True, (0, 0, 0)) # render 方法的参数依次是文本内容是否开启抗锯齿True 开启),文本颜色(黑色)
(0, 0, 0)) # render 方法的参数依次是文本内容是否开启抗锯齿True 开启),文本颜色(黑色)
author_text_rect = author_text.get_rect() author_text_rect = author_text.get_rect()
author_text_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 150) # 设置文本矩形的中心位置 author_text_rect.center = (SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 150) # 设置文本矩形的中心位置
SCREEN.blit(author_text, author_text_rect) # 使用 blit 方法将渲染后的文本表面绘制到屏幕上 SCREEN.blit(author_text, author_text_rect) # 使用 blit 方法将渲染后的文本表面绘制到屏幕上
@ -179,6 +172,7 @@ def main():
last_timestamp_barr_hit = 0 # 记录障碍物受到伤害时钟记录的上一个时刻 last_timestamp_barr_hit = 0 # 记录障碍物受到伤害时钟记录的上一个时刻
flag_barr_hit = 0 # 记录障碍物有没有受得伤害 flag_barr_hit = 0 # 记录障碍物有没有受得伤害
while run: while run:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
@ -195,6 +189,9 @@ def main():
if pygame.time.get_ticks() - last_timestamp_hit > 3000: if pygame.time.get_ticks() - last_timestamp_hit > 3000:
flag_hit = 0 flag_hit = 0
background() # 画出背景 background() # 画出背景
cloud.draw(SCREEN) # 画出云的图像 cloud.draw(SCREEN) # 画出云的图像
@ -266,15 +263,14 @@ def main():
points += 50 points += 50
barriers.remove(barrier) barriers.remove(barrier)
# 发射子弹, 并且保证每个子弹的发射间隔小于200毫秒 # 发射子弹, 并且保证每个子弹的发射间隔小于200毫秒
if (userInput[pygame.K_SPACE] and pygame.time.get_ticks() - last_timestamp >= 200) or ( if (userInput[pygame.K_SPACE] and pygame.time.get_ticks() - last_timestamp >= 200) or (userInput[pygame.K_SPACE] and last_timestamp == 0):
userInput[pygame.K_SPACE] and last_timestamp == 0):
last_timestamp = pygame.time.get_ticks() # 更新当前时间 last_timestamp = pygame.time.get_ticks() # 更新当前时间
bullets.append(Bullet(game_speed, player)) bullets.append(Bullet(game_speed, player))
pygame.display.update() pygame.display.update()
if __name__ == '__main__': if __name__ == '__main__':
menu(death_cnt = 0) menu(death_cnt = 0)
main() main()

@ -1,6 +1,5 @@
import os
import pygame import pygame
import os
# 设置游戏界面大小,保存每一个类的图像数据 # 设置游戏界面大小,保存每一个类的图像数据
SCREEN_HEIGHT = 600 SCREEN_HEIGHT = 600

Loading…
Cancel
Save