PythonStudy_打地鼠游戏代码

PythonStudy_打地⿏游戏代码# 1、导⼊模块
import pygame
from pygame.locals import *
import math
import random
# 2、游戏初始化
# 2.1设置展⽰窗⼝
pygame.init()
width , height = 640 , 480
screen = pygame.display.set_mode((width,height))
# 2.2 游戏中需要⽤到的按键keys:wasd
keys = [False,False,False,False]
# 2.3 游戏玩家的初始位置
playerpos = [100,100]
# 2.4 跟踪箭头
arrows = []
tokyo hot n0808
# 2.5 记录玩家精度:消灭的獾的数量and射出的箭头数量
acc = [0,0]
# 2.6 添加坏蛋和坏蛋计时器(隔⼀段时间出来⼀个坏蛋)
badguys = [[640,100]]
badtimer = 100
badtimer1 = 0
# 2.7 初始健康值
healthvalue = 194
# 2.8 声⾳初始化
pygame.mixer.init()
# 3、导⼊声⾳和图⽚
# 3.1、导⼊展⽰的图⽚
player = pygame.image.load(r".\\pic_lib\dude2.png")
background = pygame.image.load(r".\\pic_lib\grass.png")
阿尔玛蓝arrow = pygame.image.load(r".\\pic_lib\bullet.png")
badguyimg1 = pygame.image.load(r".\\pic_lib\badguy4.png")
badguyimg = badguyimg1    # 添加了⼀个图⽚的复制
castle = pygame.image.load(r".\\pic_lib\castle.png")
healthbar = pygame.image.load(r".\\pic_lib\healthbar.png")
health = pygame.image.load(r".\\pic_lib\health.png")
gameover = pygame.image.load(r".\\pic_lib\gameover.png")
youwin = pygame.image.load(r".\\pic_lib\youwin.png")
# 3.2、加载游戏操作的声⾳
hit = pygame.mixer.Sound(r".\\pic_lib\explode.wav")
enemy = pygame.mixer.Sound(r".\\pic_lib\enemy.wav")
shoot = pygame.mixer.Sound(r".\\pic_lib\shoot.wav")
hit.set_volume(0.05)
enemy.set_volume(0.05)
shoot.set_volume(0.05)
# 3.3 加载背景⾳乐
pygame.mixer.music.load(r".\\pic_lib\moonlight.wav")
pygame.mixer.music.play(-1,0.0)
pygame.mixer.music.set_volume(0.25)
# 4、保持循环,游戏运⾏
running = 1
exitcode = 0
while running:
污泥制砖badtimer -= 1
# 5、clear the screen before drawing it again
screen.fill(0)
# 6、draw the screen element
# 6.0 添加背景
for x in range(round( _width() ) +1):
for y in range(round( _height() ) +1):
screen.blit(background,(x*100,y*100))
# 图⽚⼩于背景
# 6.1 添加castles
screen.blit(castle,(0,30))
screen.blit(castle,(0,135))
screen.blit(castle,(0,240))
双电源自动切换装置
screen.blit(castle,(0,345))
# 6.2 添加玩家
position = _pos()
# 获取⿏标位置
angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26)) # ⿏标的位置和玩家的位置,根据三⾓定理,可得旋转⾓度函数为:atan2()
playerrot = ate(player,360-angle*57.29)
# 将玩家按所需⾓度旋转
playerpos1 = (playerpos[0]+_rect().width/2, playerpos[1]+_rect().height/2) # 计算玩家旋转⾓度后的位置
screen.blit(playerrot, playerpos1)
# 玩家移动到⿏标指定位置
# 6.3 添加箭头
for projectile in arrows:
arrow1 = ate(arrow, 360-projectile[0]*57.29)
screen.blit(arrow1, (projectile[1], projectile[2]))
for bullet in arrows:
index = 0
vlex = s(bullet[0])*10
vley = math.sin(bullet[0])*10
# 10 是箭头的速度
bullet[1] += vlex
bullet[2] += vley
if  bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
# 如果超出屏幕,删除箭头
arrows.pop(index)
index += 1
# 6.4 添加坏蛋
# 6.4.1 show the badguys
# 将坏蛋列表的坏蛋显⽰到屏幕
for badguy in badguys:
screen.blit(badguyimg,badguy)
# 6.4.2 next badguy
# 定时结束,添加坏蛋到坏蛋列表
if badtimer == 0:
badguys.append([640,random.randint(50,430)])
badtimer = 100-(badtimer1*2)
# 定时器设置:先慢后快
if badtimer1>=35:
badtimer = 35
else:
badtimer1 += 5
# 6.4.3 attack castle
index = 0
for badguy in badguys:
# 坏蛋以速度7,向前⾏进
badguy[0] -= 7
# 炸碉堡的坏蛋被删除,碉堡掉健康值
badrect = pygame._rect())

badrect.left = badguy[0]
if badrect.left<64:
hit.play()
healthvalue -= random.randint(5,20)
badguys.pop(index)
# check for collisions
index1 = 0
for bullet in arrows:
bullrect = pygame._rect())
bullrect.left = bullet[1]

lliderect(bullrect):
# 检测两个对象是否重叠
enemy.play()
acc[0] += 1
badguys.pop(index)
arrows.pop(index1)
index1 += 1
index += 1
# 6.5 添加⾎槽
screen.blit(healthbar,(5,5))
# 先画⼀个全红⾊的⽣命值条
for health1 in range(healthvalue):
screen.blit(health,(health1+8,8))
# 根据承包的⽣命值往⽣命条⾥添加绿⾊
# 7、update the screen
# 7 - 更新屏幕
# 添加⼀个计时器:游戏90秒倒计时
font = pygame.font.Font(None,24)
survivedtext = der(str(int((_ticks())/60000)).zfill(2)
+":"+str(int((_ticks())/1000%60)).zfill(2),
True,(0,0,0))
textRect = _rect()
screen.blit(survivedtext,textRect)
# 更新屏幕显⽰
pygame.display.flip()
# 8、loop through the ecents
# 8 - 检查⼀些新的事件,如果有退出命令,则终⽌程序的运⾏
# pygame⾥,⽤给按键添加事件的⽅法,来检测按键。
for event in ():
pe == pygame.QUIT:
# check if event is quit the game
pygame.quit()
exit(0)
pe == pygame.KEYDOWN:
# check if event is the X button,it is for moving
if event.key == K_w:
keys[0] = True
elif event.key == K_a:
keys[1] = True
elif event.key == K_s:
keys[2] = True
elif event.key == K_d:
keys[3] = True
pe == pygame.KEYUP:
if event.key == K_w:
keys[0] = False
elif event.key == K_a:
keys[1] = False
elif event.key == K_s:
keys[2] = False
elif event.key == K_d:
keys[3] = False
pe == pygame.MOUSEBUTTONDOWN:
# ⿏标如果被点击,得到⿏标位置并计算箭头旋转⾓度
shoot.play()
position = _pos()
acc[1] += 1
arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32]) # 9、move player
工业重型防坠器if keys[0]:
playerpos[1]-=10
elif keys[2]:
playerpos[1]+=10
elif keys[1]:
氧化挂具playerpos[0]-=10
elif keys[3]:
playerpos[0]+=10
# 10、Win/Lose check
if _ticks()>=90000:
running = 0
exitcode = 1
if healthvalue <= 0:
running = 0
exitcode = 0
if acc[1] != 0:
accuracy = format(acc[0]/acc[1]*100,'.2f')
else:
accuracy = 0
# 11、Win/Lose display
# 显⽰胜负图⽚
if running == 0 and exitcode == 0:
screen.blit(gameover,(0,0))
else:
screen.blit(youwin,(0,0))
# 显⽰胜率
pygame.font.init()
font = pygame.font.Font(None,24)
text = der("Accuracy:"+str(accuracy)+"%",True,(255,0,0))
textRect = _rect()
screen.blit(text,textRect)
# 刷新
while 1:
for event in ():
pe == pygame.QUIT:            pygame.quit()
exit(0)
pygame.display.flip()
pygame.mixer.music.stop()

本文发布于:2024-09-20 22:40:59,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/97977.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:坏蛋   位置   添加   游戏   玩家   箭头
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议