pygame

How to draw a circle in PyGame?

*爱你&永不变心* 提交于 2020-03-23 08:06:52
问题 Hi i am haveing some problem with drawing a circle ERROR: "'module' object has no attribute 'circl'" what am I dooing wrong? And also how can I put numbers in circles? ex: (first click is circle with 0 second is circle with 1 and so on) import pygame WHITE = (255, 255, 255) BLUE = ( 0, 0, 255) GREEN = ( 0, 255, 0) RED = (255, 0, 0) TEXTCOLOR = ( 0, 0, 0) (width, height) = (200, 300) running = True def main(): global running, screen pygame.init() screen = pygame.display.set_mode((width, height

Drawing a circle to a surface in pygame

强颜欢笑 提交于 2020-03-22 09:42:53
问题 I'm writing a program in pygame in which I want to draw circles on several surfaces, so that when I erase a circle (redraw it with the transparent colorkey) I get the picture that was in the layer below back. However, I seem to be stuck at an early step and can't seem to draw a circle on a surface (as opposed to the background display). Here is a minimal example: import pygame pygame.init() width = 400 height = 400 screen = pygame.display.set_mode((width, height)) surf1 = pygame.Surface(

2D游戏开发(3) pygame游戏制作

旧巷老猫 提交于 2020-03-20 11:42:52
3 月,跳不动了?>>> 一、pygame的窗口制作 开发工具 :pycharm 参考代码 : import pygame from pygame.locals import * import sys def hello_world(): pygame.init() pygame.display.set_mode((680,480)) pygame.display.set_caption("hello world") while True: for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() pygame.display.update() if __name__=="__main__": hello_world() 运行结果 : 二、游戏案例1:坦克大战 开发工具 :pycharm 导入坦克图片与背景图片, 通过方向键控制坦克的移动,speed_offset控制坦克移动的距离 参考代码 : import os,sys,pygame from pygame.locals import * def control_tank(event): speed=[x,y]=[0,0] speed_offset=1 if event.type==pygame.KEYDOWN: if event

基于pygame的游戏基础

浪尽此生 提交于 2020-03-19 21:53:01
3 月,跳不动了?>>> Pygame简介 Pygame是被设计用来写游戏的python模块集合,Pygame是在优秀的SDL库之上开发的功能性包。使用python可以导入pygame来开发具有全部特性的游戏和多媒体软件,Pygame是极度轻便的并且可以运行在几乎所有的平台和操作系统上。 1.创建一个Pygame窗口 参考代码 import pygame from pygame.locals import * import sys def hello_world(): pygame.init() pygame.display.set_mode((680,480)) pygame.display.set_caption('Hello World!') while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() if __name__ == '__main__': hello_world() 效果截图 2.创建一个能自由移动的角色 参考代码 import pygame,sys from pygame.locals import * def play_guai(): pygame.init() window_size

2015/11/3用Python写游戏,pygame入门(3):字体模块、事件显示和错误处理

谁说胖子不能爱 提交于 2020-03-18 05:41:28
游戏里面一般是肯定会出现文字显示的,即使是俄罗斯方块也应该显示分数。那么我们应该怎样来显示文字呢,今天一起学习一下pygame的font模块。 使用字体模块 pygame可以直接调用系统字体,也可以调用TTF字体。为了使用字体,首先应该创建一个Font对象,对于系统自带的字体,应该这样调用: >>> font1 = pygame.font.SysFont('arial', 16) 第一个参数是字体名,第二个参数是字号。正常情况下系统里都会有arial字体,如果没有会使用默认字体,默认字体和各个系统有关。 可以使用pygame.font.get_fonts()来获得当前系统所有可用字体: >>> pygame.font.get_fonts() ['stsong', 'rod', 'microsoftyahei', 'mingliupmingliumingliuhkscs', 'stxingkai', 'euclidmathtwo', 'impact', 'simplifiedarabicfixed', 'wingdings2', 'wingdings3', 'lucidabright', 'verdana', 'mistral', 'gigi', 'traditionalarabic', 'lucidacalligraphy',

基于pygame的游戏案例(3)

亡梦爱人 提交于 2020-03-17 17:49:54
某厂面试归来,发现自己落伍了!>>> 基于pygame的游戏案例 开发工具:python 开发平台:pycharm 游戏案例:2048 游戏规则: 键盘控制所有格子会向那个方向运动,相同数字的两个格子,相撞时数字会相加,数字每次运动时,空白处会随机刷新出一个数字的格子。当界面不可运动时(当界面全部被数字填满时),游戏结束;当界面中最大数字是2048时,游戏胜利。 三. 2048 游戏截图 代码分析 新增数字 # 新增2或4,有1/4概率产生4 def add(self): while True: p = random.randint(0, self.size * self.size - 1) if self.map[p // self.size][p % self.size] == 0: x = random.randint(0, 3) > 0 and 2 or 4 self.map[p // self.size][p % self.size] = x self.score += x break 地图向左靠拢,其他方向通过旋转 # 地图向左靠拢,其他方向的靠拢可以通过适当旋转实现,返回地图是否更新 def adjust(self): changed = False for a in self.map: b = [] last = 0 for v in a: if v != 0:

基于pygame的游戏案例(2)

本秂侑毒 提交于 2020-03-17 17:38:29
某厂面试归来,发现自己落伍了!>>> 基于pygame的游戏案例 开发工具:python 开发平台:pycharm 游戏案例:飞机大战 游戏规则: 键盘控制飞机的移动,飞机会不断发射子弹,敌机也会随机出现,没消灭一架敌机则增加一分,碰到敌机则游戏结束 二. 飞机大战 游戏截图 代码分析 定义子弹类 class Bullet(pygame.sprite.Sprite): def __init__(self, bullet_img, init_pos): pygame.sprite.Sprite.__init__(self) self.image = bullet_img self.rect = self.image.get_rect() self.rect.midbottom = init_pos self.speed = 10 def move(self): self.rect.top -= self.speed 定义玩家类 def __init__(self, plane_img, player_rect, init_pos): pygame.sprite.Sprite.__init__(self) self.image = [] # 用来存储玩家飞机图片的列表 for i in range(len(player_rect)): self.image.append(plane

基于pygame的游戏案例(1)

可紊 提交于 2020-03-17 16:41:15
某厂面试归来,发现自己落伍了!>>> 基于pygame的游戏案例 开发工具:python 开发平台:pycharm 游戏案例:贪吃蛇 游戏规则: 键盘wasd控制蛇的方向,寻找吃的东西,每吃一口草莓蛇的身子会越吃越长碰墙,咬到自己的身体或咬自己的尾巴则游戏结束。 一. 贪吃蛇游戏 游戏截图 代码分析 颜色定义: redColour = pygame.Color(255, 0, 0) blackColour = pygame.Color(0, 0, 0) whiteColour = pygame.Color(255, 255, 255) greyColour = pygame.Color(150, 150, 150) 初始化变量: snakePosition = [100,100] snakeSements = [[100,100],[80,100],[60,100]] raspberryPosition = [300,300] raspberrySpawned = 1 direction = 'right' changeDirection = direction 将蛇的身体增加一节,放在头部: snakeSements.insert(0,list(snakePosition)) 检查蛇头部的X、Y坐标是否等于草莓的X、Y坐标: if snakePosition[0] ==

四项pygame程序测试

最后都变了- 提交于 2020-03-16 17:22:06
某厂面试归来,发现自己落伍了!>>> 本次进行五项pygame的程序代码测试 测试1(简单的hello world运行框显示) 代码如下: import pygame from pygame.locals import * import sys def hello_w(): pygame.init() pygame.display.set_mode((680,480)) pygame.display.set_caption('Hello World!') while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update() if __name__ == '__main__': hello_w() 运行结果: 测试2 (坦克能进行移动(自由移动)) 代码如下: import pygame,sys from pygame.locals import * def play_tank(): pygame.init() window_size = (width,height)=(640,480) speed = [1,1] color_white = (255,255,255) screen = pygame.display

2D游戏开发——sy2.Python-pygame

霸气de小男生 提交于 2020-03-16 00:35:42
Python之pygame游戏开发 利用python中的pygame模块来进行2D游戏开发。 Pygame 是跨平台 Python 模块,专为电子游戏设计。包含图像、声音。创建在 SDL 基础上,允许实时电子游戏研发而无需被低级语言,如 C 语言或是更低级的汇编语言束缚。基于这样一个设想,所有需要的游戏功能和理念都完全简化位游戏逻辑本身,所有的资源结构都可以由高级语言提供,如 Python。 本次实验为 验证性作业 ,代码为 老师 所给。 开发坏境:Python3.7.4 + Pygame1.9.6 开发工具:PyCharm 2019.3.3 x64 基础 安装pygame,在安装好python的基础上在cmd指令中输入 pip install pygame 上图因为我已经安装了,提示我早已装好。如果网络不好,建议去官网或镜像网站下包,然后 cd 到该包所在文件夹下,输入: pip install pygame-1.9.6-cp37-cp37m-win_amd64.whl #版本按自己的来 检验是否安装python,在cmd输入 python pygame的窗口制作 设置窗口大小和窗口名称,代码如下: import pygame from pygame.locals import * import sys def hellow_world(): pygame.init()